<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jul 7, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls('html.AbstractHtmlControl');

class AbstractModule extends AbstractHtmlControl {
	
	var $_params;
	var $_name;
	
	function __construct($moduleParam, $folderName) {
		if (!$moduleParam) {
			$moduleParam = new stdClass();
		}
		if (is_array($moduleParam)) {
			RCoreImporter::cls( 'helper.RObjectHelper' );
			$this->_params = RObjectHelper::arrayToObject ( $moduleParam );
		}
		else {
			$this->_params = $moduleParam;
		}
		$this->_name = $folderName;
		
		$this->_folder = SYS_MOD_PATH . $folderName . DS;
		$this->loadLanguage ();
	}
	
	/**
	 * Get Parameters of Module
	 *
	 * @param string $name - Name of Parameter
	 * @return value of Parameter
	 */
	function getParam($name, $default = null) {
		if ($default === null) {
			return $this->_params->$name;
		}
		
		$val = $this->_params->$name;
		if ($val === null) {
			return $default;
		}
		return $val;
	}
	
	/**
	 * Set parameter
	 *
	 * @param string $name
	 * @param string $value
	 */
	function setParam($name, $value) {
		$this->_params->$name = $value;
	}
	
	/**
	 * Set all params
	 *
	 * @param mix $arrayOrObject
	 */
	function setParams($arrayOrObject) {
		RubikHelper::setProperties($this->_params, $arrayOrObject);
	}
	
	/**
	 * Get the encoded param string
	 *
	 * @return string
	 */
	function getParamEncoded() {
		$params = serialize ( $this->_params );
		$params = base64_encode ( $params );
		return $params;
	}
	
	/**
	 * Function to check Module view or not
	 * Please implements if you want special treat
	 * NOTE: this function can be OVERRIDE for general perpose.
	 *
	 * @return true|false Module showed if return true
	 */
	function isView() {
		return true;
	}
	
	/**
	 * Load langguage for module
	 *
	 * @param string $moduleName - Name of Module
	 * 
	 */
	function loadLanguage($langFile = null) {
		$langPath = 'mod.' . $langName;
		return parent::loadLanguage ( $langPath );
	}
	
	/**
	 * print the mudule content
	 *
	 */
	function printModule() {
		if ($_GET ["showmoduleinfo"]) {
			echo '<div style="background: #EEEEEE; border:1px dotted black; color:#770000; opacity:0.5;">';
			echo '<div style="position: absolute; z-index: 10000; background: #FFFFFF; border:1px solid black; color:red;">';
			echo $this->_name;
			echo '</div>';
			$this->display ();
			echo '</div>';
			return;
		}
		
		$cacheTimeLife = $this->getParam ( 'cache' );
		if ($this->getParam ( 'lazy' )) {
			$cacheId = $this->_name."_".$this->hash ();
			// If the cache param is ennable
			if ($cacheTimeLife && SysCache::startCache ( $cacheId, 'module', $cacheTimeLife )) {
				return;
			}
			SysCache::endCache ( 'module', false, $cacheTimeLife);
			
			$moduleObj = base64_encode ( serialize ( $this ) );
			$lazyMode = $this->getParam ( 'lazy' );
			$link = SYS_ROOT_URL . "core/runAlone/moduleResponser.php?name={$this->_name}&module=$moduleObj";
			switch ($lazyMode) {
				case 'ajax' :
					$modId = "mod_" . md5 ( $moduleObj );
					echo '<div id="' . $modId . '"><img alt="running" src="' . SYS_ROOT_URL . 'files/images/running.gif" /></div>';
					$script = "
						$(document).ready(function(){
							$('#{$modId}').load('{$link}');
						});
					";
					SysResponse::addJavascriptContent ( $script );
					break;
				case 'javascript' :
				default :
					echo "<script type=\"text/javascript\" src=\"{$link}\" ></script>";
					break;
			}
			return;
		}
		
		// If the cache param is ennable
		if ($cacheTimeLife) {
			$cacheId = $this->_name."_".$this->hash ();
			if (SysCache::startCache ( $cacheId, 'module', $cacheTimeLife)) {
				return;
			}
			$saveCache = $this->display () !== false;
			SysCache::endCache ('module', $saveCache, $cacheTimeLife);
			return;
		}
		$this->display ();
	}
	
	/**
	 * Display
	 * @abstract 
	 *
	 */
	function display() {
		die ( "This function must be implement in the class: <b>" . get_class ( $this ) . "</b>" );
	}
	
	/**
	 * Get hash value for creating cache.
	 * NOTE: this function can be OVERRIDE for general perpose
	 *
	 */
	function hash() {
		$hash = $this->_name;
		foreach ( $this->_params as $key => $value ) {
			$hash .= "{$key}->{$value}";
		}
		
		return md5 ( $hash );
	}
	
	/**
	 * Get the module name of current module
	 *
	 * @return string
	 */
	function getName() {
		return $this->_name;
	}
	
	/**
	 * Load CSS for module
	 *
	 * @param string $cssFile
	 */
	function loadCSS($cssFile) {
		return SysTemplate::loadModuleCSS($cssFile, $this->_name);
	}
	
	/**
	 * Load js file for module
	 *
	 * @param str $jsFile
	 */
	function loadJs($jsFile) {
		return SysTemplate::loadModuleJS($jsFile, $this->_name);
	}
	
	/**
	 * Get the content of current module
	 * We can use this function in some case we don't want to print the module directly.
	 *
	 * @return string
	 */
	function getContent() {
		ob_start();
		$this->display();
		$result = ob_get_contents();
		ob_clean();
		
		return $result;
	}
}

?>
